#!/bin/bash
echo 'begin...'
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi
DATA_DIR=$DEPLOY_DIR/data
if [ ! -d $DATA_DIR ]; then
    mkdir $DATA_DIR
fi
export LANG=zh_CN.UTF-8
#export DOCKER_JAVA_OPTS="-XX:MaxNewSize=500M -XX:PermSize=64m -XX:MaxPermSize=256M -XX:SurvivorRatio=4 -Xss256K"
if [ ! -z "$ZOOKEEPER_ADDRESS" ]; then
    sed -i "s#^hosts=.*#hosts=$ZOOKEEPER_ADDRESS#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/zoo.properties;
fi
if [ ! -z "$DOMAIN_URL" ]; then
    sed -i "s#^domain=.*#domain=$DOMAIN_URL#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/application.properties;
fi
if [ ! -z "$EMAIL_MONITOR_ON" ]; then
    sed -i "s#^EMAIL_MONITOR_ON=.*#EMAIL_MONITOR_ON=$EMAIL_MONITOR_ON#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/application.properties;
fi
if [ ! -z "$EMAIL_HOST" ]; then
    sed -i "s#^EMAIL_HOST=.*#EMAIL_HOST=$EMAIL_HOST#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/application.properties;
fi
if [ ! -z "$EMAIL_HOST_PASSWORD" ]; then
    sed -i "s#^EMAIL_HOST_PASSWORD=.*#EMAIL_HOST_PASSWORD=EMAIL_HOST_PASSWORD#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/application.properties;
fi
if [ ! -z "$EMAIL_HOST_USER" ]; then
    sed -i "s#^EMAIL_HOST_USER=.*#EMAIL_HOST_USER=$EMAIL_HOST_USER#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/application.properties;
fi
if [ ! -z "$EMAIL_PORT" ]; then
    sed -i "s#^EMAIL_PORT=.*#EMAIL_PORT=$EMAIL_PORT#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/application.properties;
fi
if [ ! -z "$DEFAULT_FROM_EMAIL" ]; then
    sed -i "s#^DEFAULT_FROM_EMAIL=.*#DEFAULT_FROM_EMAIL=$DEFAULT_FROM_EMAIL#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/application.properties;
fi
echo -e "Starting  ...\c"
catalina.sh run